<!--
 * @Description: 工序模型
 * 
 * @Author: XiongYaoYuan<xiongyaoyuan>
 * @Date: 2023-06-05 14:28:41
 * @FilePath: /yuxiuproweb/src/views/process/production/index.vue
-->
<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
    >
      <el-form-item label="名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd()"
          v-hasPermi="['pro:process:add']"
          >新增</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="processList"
      row-key="processId"
      default-expand-all
      :tree-props="{ children: 'children' }"
      :max-height="maxHeight + 50"
    >
      <!-- <el-table-column label="id" align="center" prop="processId" /> -->
      <el-table-column label="名称" prop="name" min-width="150" />
      <el-table-column label="排序" prop="sort" align="center" />
      <el-table-column label="工序价格" align="center" prop="price" />
      <el-table-column label="影响因素" align="center" prop="influenceFactor">
        <template slot-scope="{ row }">
          {{ getTypeName(dict.type.md_process_factor, row.influenceFactor) }}
        </template>
      </el-table-column>
      <el-table-column label="是否启用" align="center" prop="enableFlag">
        <template slot-scope="{ row }">{{
          row.enableFlag === "Y" ? "是" : row.enableFlag === "N" ? "否" : ""
        }}</template>
      </el-table-column>
      <el-table-column
        label="耗时(秒)"
        align="center"
        prop="requiredTime"
      ></el-table-column>
      <el-table-column
        label="距交单时间(天)"
        align="center"
        prop="hoursOfPresentation"
      />
      <!-- <el-table-column label="父级id" align="center" prop="pid" />
      <el-table-column label="排序" align="center" prop="sort" /> -->
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        min-width="180"
        fixed="right"
      >
        <template slot-scope="scope">
          <el-button
            v-if="!scope.row.pid"
            size="mini"
            type="text"
            icon="el-icon-plus"
            @click="handleAdd(scope.row.processId)"
            >添加</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['pro:process:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['pro:process:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    /> -->

    <!-- 添加或修改工序模型对话框 -->
    <el-dialog
      :close-on-click-modal="false"
      :title="title"
      :visible.sync="open"
      width="550px"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="115px">
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input-number
            v-model="form.sort"
            controls-position="right"
            placeholder="请输入排序"
            :precision="0"
            :min="0"
          ></el-input-number>
        </el-form-item>
        <template v-if="pid">
          <el-form-item label="工序价格" prop="price">
            <el-input-number
              v-model="form.price"
              controls-position="right"
              placeholder="请输入价格"
              :precision="3"
              :min="0"
            ></el-input-number>
          </el-form-item>
          <el-form-item label="影响因素" prop="influenceFactor">
            <el-radio-group v-model="form.influenceFactor">
              <el-radio
                v-for="item in dict.type.md_process_factor"
                :key="item.value"
                :label="item.value"
                >{{ item.label }}</el-radio
              >
            </el-radio-group>
          </el-form-item>
          <el-form-item label="是否启用" prop="enableFlag">
            <el-switch
              v-model="form.enableFlag"
              active-value="Y"
              inactive-value="N"
            ></el-switch>
          </el-form-item>
          <el-form-item label="耗时(秒)" prop="requiredTime">
            <el-input-number
              v-model="form.requiredTime"
              controls-position="right"
              placeholder="请输入耗时"
              :precision="0"
              :min="0"
            ></el-input-number>
          </el-form-item>
          <el-form-item label="距交单时间(天)" prop="hoursOfPresentation">
            <el-input-number
              v-model="form.hoursOfPresentation"
              controls-position="right"
              placeholder="请输入距交单时间"
              :precision="0"
              :min="0"
            ></el-input-number>
          </el-form-item>
        </template>
        <!-- <el-form-item label="父级id" prop="pid">
          <el-input v-model="form.pid" placeholder="请输入父级id" />
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listProcess,
  getProcess,
  delProcess,
  addProcess,
  updateProcess,
} from "@/api/process/production";
import { delDepartTree } from "./helper";
import commonMix from "@/utils/mixins/common";

export default {
  name: "Process",
  dicts: ["md_process_factor"],
  mixins: [commonMix],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 工序模型表格数据
      processList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 1000,
        name: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [{ required: true, message: "名称不能为空", trigger: "blur" }],
        price: [
          { required: true, message: "工序价格不能为空", trigger: "blur" },
        ],
        influenceFactor: [
          { required: true, message: "影响因素不能为空", trigger: "change" },
        ],
        enableFlag: [
          { required: true, message: "是否启用不能为空", trigger: "change" },
        ],
        requiredTime: [
          { required: true, message: "耗时不能为空", trigger: "blur" },
        ],
        hoursOfPresentation: [
          { required: true, message: "距交单时间不能为空", trigger: "blur" },
        ],
      },
      pid: null,
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询工序模型列表 */
    getList() {
      this.loading = true;
      listProcess(this.queryParams).then((response) => {
        this.processList = delDepartTree(
          response.rows.map((e) => ({
            ...e,
            processId: `${e.processId}`,
          }))
        );
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.pid = null;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        processId: null,
        name: null,
        price: undefined,
        influenceFactor: null,
        enableFlag: "Y",
        requiredTime: this.pid !== null ? 0 : null,
        hoursOfPresentation: this.pid !== null ? 0 : null,
        pid: null,
        sort: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd(pid) {
      this.reset();
      this.pid = pid;
      this.open = true;
      this.title = "添加工序模型";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const processId = row.processId || this.ids;
      getProcess(processId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.pid = row.pid;
        this.title = "修改工序模型";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.processId != null) {
            updateProcess(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.pid = null;
              this.getList();
            });
          } else {
            this.form.pid = this.pid;
            addProcess(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.pid = null;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const processIds = row.processId || this.ids;
      this.$modal
        .confirm('是否确认删除工序模型编号为"' + processIds + '"的数据项？')
        .then(function () {
          return delProcess(processIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },

  },
};
</script>
